package person.wend.javalearnexample.map;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapExample {

    public static void main(String[] args) {
        TreeMap<String, Integer> treeMap = new TreeMap<>();
        treeMap.put("apple", 1);
        treeMap.put("banana", 2);
        treeMap.put("cherry", 3);

        System.out.println("Value of 'apple': " + treeMap.get("apple"));
        System.out.println("First key: " + treeMap.firstKey());
        System.out.println("Last key: " + treeMap.lastKey());

        treeMap.remove("banana");

        System.out.println("After removal: " + treeMap);
    }
}

class CustomComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

class TreeMapWithComparatorExample {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMapWithComparator = new TreeMap<>(new CustomComparator());
        treeMapWithComparator.put(1, "One");
        treeMapWithComparator.put(3, "Three");
        treeMapWithComparator.put(2, "Two");

        System.out.println("TreeMap with custom comparator: " + treeMapWithComparator);
    }
}
